package com.zjhome.cloudnote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zjhome.cloudnote.constant.ErrorCode;
import com.zjhome.cloudnote.domain.RecycleInfo;
import com.zjhome.cloudnote.param.AckResult;
import com.zjhome.cloudnote.service.RecycleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 回收站控制器
 * 
 * @author zjhome
 *
 */
@RestController
@Api("回收站管理")
@RequestMapping("/recycle")
public class RecycleController extends BaseController {
	@Autowired
	private RecycleService	recycleService;
	
	/**
	 * 文件夹列表查询
	 * 
	 * @param did	父文件夹id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/list")
	@ApiOperation(value = "回收站列表")
	public List<RecycleInfo> getRecycleItems(RecycleInfo recycleInfo) throws Exception {		
		recycleInfo.setUid(this.getUserId());
		List<RecycleInfo> list = recycleService.selectRecycleByPara(recycleInfo);
		return list;
	}

	/**
	 * 删除（从回收站中彻底删除）
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除")
	public AckResult deleteItem(String id) throws Exception {
		// id 格式通过 '_' 间隔
		String[] ids = id.split("_");
				
		int deleteRes = recycleService.deleteRes(ids, this.getUserId());
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
	
	/**
	 * 还原（从回收站中恢复）
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/restore")
	@ApiOperation(value = "还原")
	public AckResult restoreItem(String id) throws Exception {
		// id 格式通过 '_' 间隔
		String[] ids = id.split("_");
		
		int deleteRes = recycleService.restoreRes(ids, this.getUserId());
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
	
	/**
	 * 清空回收站
	 * 
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/clear")
	@ApiOperation(value = "清空回收站")
	public AckResult clearRecycle() throws Exception {
				
		int deleteRes = recycleService.truncateRecycle(this.getUserId());
		
		return new AckResult(ErrorCode.ERR_NORMAL);
	}
}
